package com.liuzhilong.infrastructure.basic.file;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import static com.liuzhilong.infrastructure.basic.file.FileTestConstant.*;

public class FileSimpleIO {
    public static final String workPath = "C:/Users/21012273/Desktop/temp";
    public static final long loopCount = 100 * 100 * 100 * 10;


    /**
     * <h2>创建一个新的文件</h2>
     * <p>
     *     创建一个新的文件
     * </p>
     */
    public static File createFile(String fileName) throws IOException {
        String path = workPath+ File.separator + fileName;
        System.out.println(path);
        File newFile = new File(path);
        if(!newFile.exists()){
            newFile.createNewFile();
        }
        return newFile;
    }
    /*
     * 打印一个文件的相关属性。
     */
    public static final void printFileInfo(File file) throws IOException {
        System.out.println("****************************************");
        System.out.println("文件名称:"+file.getName());
        System.out.println("文件定义路径:"+file.getPath());
        System.out.println("文件定义绝对路径:"+file.getAbsolutePath());
        System.out.println("文件绝对路径:"+file.getCanonicalPath());
        System.out.println("文件大小:"+file.length());
        System.out.println("****************************************");
    }

    /**
     * 写入文件数据 普通流
     * @param file
     */
    public static final void writeByStream(File file) throws IOException {

        long start = System.currentTimeMillis();
        System.out.println("开始写入:"+start);
        try(OutputStream outputStream = new FileOutputStream(file)) {
            for (int i = 0; i <100*100*100 ; i++) {
                outputStream.write(fullChar.getBytes(StandardCharsets.UTF_8));
            }
            //刷盘
            outputStream.flush();
        }
        System.out.println("结束写入:"+System.currentTimeMillis()+",耗时"+(start-System.currentTimeMillis()));

    }

    /**
     * 按照普通流的方式进行读取
     * @param file
     * @throws IOException
     */
    public static final void readByStream(File file) throws IOException {
        long start = System.currentTimeMillis();
        System.out.println("开始读取:"+start);
        byte[] bytes = new byte[fullChar.getBytes(StandardCharsets.UTF_8).length];
        try(FileInputStream inputStream = new FileInputStream(file)) {
            while (inputStream.read(bytes)>0){
                //读出数据来。
            }
        }
        System.out.println("结束读取:"+System.currentTimeMillis()+",耗时"+(System.currentTimeMillis()-start));

    }

    /**
     * 写入文件数据-buffer
     * @param file
     */
    public static final void writeByBuffer(File file) throws IOException {

        long start = System.currentTimeMillis();
        System.out.println("开始写入:"+start);
        try(OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file),fullChar.getBytes(StandardCharsets.UTF_8).length*100*100*100)) {

            for (int i = 0; i <loopCount ; i++) {
                outputStream.write(fullChar.getBytes(StandardCharsets.UTF_8));
            }
            //刷盘
            outputStream.flush();
        }
        System.out.println("结束写入:"+System.currentTimeMillis()+",耗时"+(start-System.currentTimeMillis()));

    }
    /**
     * 按照Buffer流的方式进行读取
     * @param file
     * @throws IOException
     */
    public static final void readByBuffer(File file) throws IOException {
        long start = System.currentTimeMillis();
        System.out.println("开始读取:"+start);
        byte[] bytes = new byte[fullChar.getBytes(StandardCharsets.UTF_8).length];
        try(InputStream inputStream = new BufferedInputStream(new FileInputStream(file),fullChar.getBytes(StandardCharsets.UTF_8).length*100*100*100)) {
            while (inputStream.read(bytes)>0){
                //读出数据来。
            }
        }
        System.out.println("结束读取:"+System.currentTimeMillis()+",耗时"+(System.currentTimeMillis()-start));

    }


    /**
     * 写入文件数据-channel
     * @param file
     */
    public static final void writeByChannel(File file) throws IOException {

        long start = System.currentTimeMillis();
        System.out.println("开始写入:"+start);
        FileChannel fileChannel= new FileOutputStream(file).getChannel();
        //设置一个buffer 可以容纳100个fullchar
        ByteBuffer byteBuffer = ByteBuffer.allocate(fullChar.getBytes(StandardCharsets.UTF_8).length*100*100*100);
        for (int i = 0; i <loopCount ; i++) {
            if(byteBuffer.position()==byteBuffer.limit()){
                //满了
                //切换模式
                byteBuffer.flip();
                if(byteBuffer.hasRemaining()){
                    fileChannel.write(byteBuffer);
                }
                byteBuffer.clear();
            }
            byteBuffer.put(fullChar.getBytes(StandardCharsets.UTF_8));
        }
        //最后一次
        byteBuffer.flip();
        if(byteBuffer.hasRemaining()){
            fileChannel.write(byteBuffer);
        }
        byteBuffer.clear();
        fileChannel.close();
            //刷盘
        System.out.println("结束写入:"+System.currentTimeMillis()+",耗时"+(start-System.currentTimeMillis()));

    }
    /**
     * 按照Channel流的方式进行读取
     * @param file
     * @throws IOException
     */
    public static final void readByChannel(File file) throws IOException {
        long start = System.currentTimeMillis();
        System.out.println("开始读取:"+start);
        FileChannel fileChannel =    new FileInputStream(file).getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(fullChar.getBytes(StandardCharsets.UTF_8).length*100*100);
        while (  fileChannel.read(byteBuffer)>0) {
            byteBuffer.clear();
        }

        System.out.println("结束读取:"+System.currentTimeMillis()+",耗时"+(System.currentTimeMillis()-start));

    }
    public static void main(String[] args) throws IOException {
        File test = createFile("test" + System.currentTimeMillis());
        printFileInfo(test);
        writeByBuffer(test);
        readByBuffer(test);
        printFileInfo(test);
        //

    }


}
